﻿<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" CodeBehind="CaptchaValidator.aspx.cs" Inherits="CaptchaSample.CaptchaValidator" %>
<%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">

        var onValidationPassCallback = function () {
            alert("SUCCESS - u should override this callback");
        }

        var onValidationFailureCallback = null;

        function ReceiveServerData(arg, context) {
            if (arg != "true") {
                document.getElementById("textToValidate").value = "";
                refreshCaptcha();
                if (onValidationFailureCallback) {
                    onValidationFailureCallback();
                }
            }
            else {
                document.getElementById("btnRefresh").disabled = true;
                document.getElementById("textToValidate").disabled = true;

                if (onValidationPassCallback) {
                    onValidationPassCallback();
                }
            }
        }

        function getChalenge() {
            return document.getElementById("textToValidate").value;
        }

        function refreshCaptcha() {
            __doPostBack('<%=UpdatePanel.ClientID%>', 'refreshCaptcha');
        }

        var validated = false;
        var lastChalenge = "";

        window["validate"] = function (onValidationPass, onValidationFailure) {

            if (onValidationPass) {
                onValidationPassCallback = onValidationPass;
            }

            if (onValidationFailure) {
                onValidationFailureCallback = onValidationFailure;
            }

            if (!validated) {
                var chalenge = getChalenge();
                if (chalenge && chalenge != lastChalenge) {
                    lastChalenge = chalenge;
                    CallServer(chalenge);
                }
            }

            return false;

        };

    </script>

    <style type="text/css">
        .captchaFrame
        {
           padding: 20px 15px 20px 15px;
           width: 300px;
           height: 110px;
        }
        
        .captchaTitle
        {
            font-family:Arial;
            font-size: 8pt;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
    <div id="divCaptcha" class="captchaFrame">
        <div class="captchaTitle">Type the characters you see in the picture below</div>
        <table style="width: 100%; height: 50px; margin-top:5px;">
        <tr>
            <td>
                <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
                    <ContentTemplate>
                        <cc1:CaptchaControl ID="CaptchaControl1" runat="server" Height="50px" 
                                Width="180px" CaptchaLength="5" BackColor="White" 
                                EnableViewState="False" />   
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
            <td>
                <img id="btnRefresh" src="Images/refresh.png" title="refresh" alt="refresh" height="20px" width="20px" onclick="refreshCaptcha();" style="border:0"/>
            </td>
        </tr>
        </table>
        <br />
        <input id="textToValidate" />
    </div>
    
    </form>
</body>
</html>
